package com.smartleanx.module.hrm.controller.admin.examine.examineDimensionIndex;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import static com.smartleanx.framework.common.pojo.CommonResult.success;

import com.smartleanx.framework.excel.core.util.ExcelUtils;

import com.smartleanx.framework.operatelog.core.annotations.OperateLog;
import static com.smartleanx.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.smartleanx.module.hrm.controller.admin.examine.examineDimensionIndex.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineDimensionIndex.ExamineDimensionIndexDO;
import com.smartleanx.module.hrm.service.examine.examineDimensionIndex.ExamineDimensionIndexService;

@Tag(name = "管理后台 - 考核维度中间")
@RestController
@RequestMapping("/hrm/examine-dimension-index")
@Validated
public class ExamineDimensionIndexController {

    @Resource
    private ExamineDimensionIndexService examineDimensionIndexService;

    @PostMapping("/create")
    @Operation(summary = "创建考核维度中间")
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension-index:create')")
    public CommonResult<Integer> createExamineDimensionIndex(@Valid @RequestBody ExamineDimensionIndexSaveReqVO createReqVO) {
        return success(examineDimensionIndexService.createExamineDimensionIndex(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考核维度中间")
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension-index:update')")
    public CommonResult<Boolean> updateExamineDimensionIndex(@Valid @RequestBody ExamineDimensionIndexSaveReqVO updateReqVO) {
        examineDimensionIndexService.updateExamineDimensionIndex(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考核维度中间")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension-index:delete')")
    public CommonResult<Boolean> deleteExamineDimensionIndex(@RequestParam("id") Integer id) {
        examineDimensionIndexService.deleteExamineDimensionIndex(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考核维度中间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension-index:query')")
    public CommonResult<ExamineDimensionIndexRespVO> getExamineDimensionIndex(@RequestParam("id") Integer id) {
        ExamineDimensionIndexDO examineDimensionIndex = examineDimensionIndexService.getExamineDimensionIndex(id);
        return success(BeanUtils.toBean(examineDimensionIndex, ExamineDimensionIndexRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考核维度中间分页")
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension-index:query')")
    public CommonResult<PageResult<ExamineDimensionIndexRespVO>> getExamineDimensionIndexPage(@Valid ExamineDimensionIndexPageReqVO pageReqVO) {
        PageResult<ExamineDimensionIndexDO> pageResult = examineDimensionIndexService.getExamineDimensionIndexPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExamineDimensionIndexRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考核维度中间 Excel")
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension-index:export')")
    @OperateLog(type = EXPORT)
    public void exportExamineDimensionIndexExcel(@Valid ExamineDimensionIndexPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExamineDimensionIndexDO> list = examineDimensionIndexService.getExamineDimensionIndexPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考核维度中间.xlsx", "数据", ExamineDimensionIndexRespVO.class,
                        BeanUtils.toBean(list, ExamineDimensionIndexRespVO.class));
    }

}